/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.beynet.docs.ui;

import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.security.auth.Subject;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import org.beynet.docs.format.Collection;

/**
 *
 * @author beynet
 */
public class RemoteCollectionsTree extends javax.swing.JDialog {

    private final DefaultTreeModel model;
    private GSyncMainFrame parent ;
    private StringBuilder  path;

    /**
     * Creates new form RemoteCollectionsTree
     */
    public RemoteCollectionsTree(Frame parent, boolean modal) {
        super(parent, modal);
        this.parent = (GSyncMainFrame)parent ;
        model = new DefaultTreeModel(null);
        
        CollectionTreeNode rootCollection = new CollectionTreeNode(new Collection(),model);
        model.setRoot(rootCollection);
        initComponents();
        remoteCollectionsTree.collapseRow(0);
        // mouse listener to retrieve action on the tree
        MouseListener ml = new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                int selRow = remoteCollectionsTree.getRowForLocation(e.getX(), e.getY());
                TreePath selPath = remoteCollectionsTree.getPathForLocation(e.getX(), e.getY());
                if (selRow != -1) {
                    if (e.getClickCount() == 2) {
                        onDoucleClick(selPath);
                    }
                }
            }
        };
        remoteCollectionsTree.addMouseListener(ml);
    }
    
    private void makePath(TreePath selPath,StringBuilder path) {
        if (selPath.getParentPath()!=null && selPath.getParentPath().getParentPath()!=null) {
            makePath(selPath.getParentPath(), path);
            path.append("/");
        }
        path.append(selPath.getLastPathComponent());
    }
    
    /**
     * called when a double click on a node is done
     * @param selPath 
     */
    private void onDoucleClick(TreePath selPath) {
        if (selPath.getParentPath()==null) return;
        path = new StringBuilder();
        makePath(selPath,path);
        this.setVisible(false);
    }
    
    public String getSelectedPath() {
        if (path==null) return null;
        return(path.toString());
    }
    
    public TreeModel getModel() {
        return (model);
    }
   

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        remoteCollectionsTree = new javax.swing.JTree();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        remoteCollectionsTree.setModel(model);
        remoteCollectionsTree.addTreeWillExpandListener(new javax.swing.event.TreeWillExpandListener() {
            public void treeWillExpand(javax.swing.event.TreeExpansionEvent evt)throws javax.swing.tree.ExpandVetoException {
                remoteCollectionsTreeTreeWillExpand(evt);
            }
            public void treeWillCollapse(javax.swing.event.TreeExpansionEvent evt)throws javax.swing.tree.ExpandVetoException {
            }
        });
        jScrollPane1.setViewportView(remoteCollectionsTree);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 402, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void remoteCollectionsTreeTreeWillExpand(javax.swing.event.TreeExpansionEvent evt) throws javax.swing.tree.ExpandVetoException {//GEN-FIRST:event_remoteCollectionsTreeTreeWillExpand
        // TODO add your handling code here:
        TreePath selPath = evt.getPath();
        Object obj = selPath.getLastPathComponent();
        if (obj instanceof CollectionTreeNode) {
            CollectionTreeNode currentNode = (CollectionTreeNode) obj;
            Subject.doAs(parent.getLoginContext().getSubject(), currentNode);
        }
    }//GEN-LAST:event_remoteCollectionsTreeTreeWillExpand

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree remoteCollectionsTree;
    // End of variables declaration//GEN-END:variables
}
